package com.codeest.geeknews.ui.main.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.codeest.geeknews.R;
import com.codeest.geeknews.util.okhttp.DownloadInfo;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/7/19.
 */

public class DownloadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<DownloadInfo> mList;
    private LayoutInflater inflater;

    public DownloadAdapter(Context mContext, List<DownloadInfo> mList) {
        this.mContext = mContext;
        this.mList = mList;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DownloadViewHolder(inflater.inflate(R.layout.item_download, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        DownloadInfo downloadInfo = mList.get(position);
        DownloadViewHolder topHolder = (DownloadViewHolder) holder;
        int progress = (int) (downloadInfo.getProgress() * 1.0 / downloadInfo.getTotal() * topHolder.progressBar.getMax());
        @SuppressLint("DefaultLocale") String progressText = String.format("%d%%", progress);
        topHolder.progressBar.setProgress(progress);
        topHolder.tv_progress.setText(progressText);
        topHolder.tv_title.setText(downloadInfo.getFileName());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class DownloadViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.pBar)
        ProgressBar progressBar;
        @BindView(R.id.tv_progress)
        TextView tv_progress;
        @BindView(R.id.tv_title)
        TextView tv_title;

        public DownloadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void update(final DownloadInfo newdata) {
        //notifyItemChanged(mList.indexOf(newdata));
        //notifyItemChanged(mList.indexOf(newdata), newdata);
        if (!mList.contains(newdata)) {
            mList.add(newdata);
        }
        notifyDataSetChanged();
    }

    public void update(final List<DownloadInfo> newLists) {
        if (mList == null) {
            mList = newLists;
            notifyItemRangeInserted(0, newLists.size());
        } else {
            DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffUtil.Callback() {
                @Override
                public int getOldListSize() {
                    return mList.size();
                }

                @Override
                public int getNewListSize() {
                    return newLists.size();
                }

                @Override
                public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
                    DownloadInfo old = mList.get(oldItemPosition);
                    DownloadInfo new_ = newLists.get(newItemPosition);
                    return old.getUrl().equals(new_.getUrl());
                }

                @Override
                public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
                    DownloadInfo old = mList.get(oldItemPosition);
                    DownloadInfo new_ = newLists.get(newItemPosition);
                    return Objects.equals(old.getUrl(), new_.getUrl())
                            && Objects.equals(old.getFileName(), new_.getFileName())
                            && old.getTotal() == new_.getTotal()
                            && old.getProgress() == new_.getProgress();
                }
            });
            mList = newLists;
            diffResult.dispatchUpdatesTo(this);
        }
    }
}
